﻿package com.gengine.resource
{
    import com.gengine.debug.*;
    import com.gengine.resource.loader.*;
    import flash.events.*;
    import flash.system.*;
    import flash.utils.*;

    public class ConfigManager extends EventDispatcher
    {
        private var _isLoaded:Boolean = false;
        private var _configDic:Dictionary;
        private static var _instance:ConfigManager;

        public function ConfigManager()
        {
            if (_instance != null)
            {
                throw new Error("ConfigManager 单例");
            }
            this._isLoaded = false;
            return;
        }// end function

        public function load(param1:String) : void
        {
            var _loc_2:* = new DataLoader();
            _loc_2.addEventListener(Event.COMPLETE, this.onLoadCompleteHandler);
            _loc_2.addEventListener(ProgressEvent.PROGRESS, this.onLoadProgressHandler);
            _loc_2.addEventListener(ErrorEvent.ERROR, this.onErrorHandler);
            _loc_2.load(param1, null);
            return;
        }// end function

        private function onLoadCompleteHandler(event:Event) : void
        {
            var memory:int;
            var loader:DataLoader;
            var byteArray:ByteArray;
            var tim:Number;
            var event:* = event;
            try
            {
                memory = System.totalMemory;
                this._isLoaded = true;
                loader = event.target as DataLoader;
                byteArray = loader.bytesData;
                byteArray.position = 0;
                tim = getTimer();
                byteArray.uncompress();
                Log.debug("解压Data byteArray耗时间", getTimer() - tim);
                tim = getTimer();
                this._configDic = byteArray.readObject() as Dictionary;
                Log.debug("data byteArray.readObject耗时间", getTimer() - tim);
                dispatchEvent(event);
                Log.system("系统总内存:", System.totalMemory, "解压data内存增加:", System.totalMemory - memory);
            }
            catch (e:Error)
            {
                _isLoaded = false;
                _configDic = null;
                dispatchEvent(new Event(IOErrorEvent.IO_ERROR));
            }
            return;
        }// end function

        private function onLoadProgressHandler(event:ProgressEvent) : void
        {
            dispatchEvent(event);
            return;
        }// end function

        private function onErrorHandler(event:ErrorEvent) : void
        {
            this._isLoaded = false;
            dispatchEvent(event);
            return;
        }// end function

        public function getObjectByFileName(param1:String, param2:Boolean = true) : Object
        {
            var _loc_3:ByteArray = null;
            var _loc_4:Object = null;
            if (this.hasFile(param1))
            {
                _loc_3 = this._configDic[param1].content as ByteArray;
                _loc_3.position = 0;
                _loc_4 = _loc_3.readObject().root;
                if (param2)
                {
                    delete this._configDic[param1];
                }
                return _loc_4;
            }
            return null;
        }// end function

        public function getStringByFileName(param1:String) : String
        {
            if (this.hasFile(param1))
            {
                return this._configDic[param1].content;
            }
            return null;
        }// end function

        public function getJSONByFileName(param1:String, param2:Boolean = true) : Object
        {
            var _loc_3:ByteArray = null;
            var _loc_4:Object = null;
            if (this.hasFile(param1))
            {
                _loc_3 = this._configDic[param1].content as ByteArray;
                _loc_3.position = 0;
                _loc_4 = _loc_3.readObject();
                if (param2)
                {
                    delete this._configDic[param1];
                }
                return _loc_4;
            }
            return null;
        }// end function

        public function hasFile(param1:String) : Boolean
        {
            if (this._isLoaded == false)
            {
                return false;
            }
            return this._configDic[param1] != null;
        }// end function

        public function getByteArrayByFileName(param1:String, param2:Boolean = true) : ByteArray
        {
            var _loc_3:ByteArray = null;
            if (this.hasFile(param1))
            {
                _loc_3 = this._configDic[param1].content as ByteArray;
                if (param2)
                {
                    delete this._configDic[param1];
                }
                return _loc_3;
            }
            return null;
        }// end function

        public static function get instance() : ConfigManager
        {
            if (_instance == null)
            {
                _instance = new ConfigManager;
            }
            return _instance;
        }// end function

    }
}
